home *** CD-ROM | disk | FTP | other *** search
-
- /*⌐ Copyright 1988-1991 UserLand Software, Inc. All Rights Reserved.*/
-
-
-
- #include "appletinternal.h"
- #include "quickdraw.h"
- #include "ops.h"
-
-
- #ifdef MPWC
-
- #include <osevents.h>
-
- #endif
-
-
-
-
-
-
- void initmacintosh () {
-
- /*
- the magic stuff that every Macintosh application needs to do
- before doing anything else.
- */
-
- register short i;
-
- MaxApplZone ();
-
- for (i = 1; i < 11; i++)
- MoreMasters ();
-
- InitGraf (&quickdrawglobal (thePort));
-
- InitFonts ();
-
- FlushEvents (everyEvent, 0);
-
- InitWindows ();
-
- InitMenus ();
-
- TEInit ();
-
- InitDialogs (nil);
-
- InitCursor ();
-
- for (i = 1; i <= 5; i++) { /*register with Multifinder*/
-
- EventRecord ev;
-
- EventAvail (everyEvent, &ev); /*see TN180 -- splash screen*/
- } /*for*/
- } /*initmacintosh*/
-
-
- short minint (x, y) short x, y; {
-
- if (x < y)
- return (x);
- else
- return (y);
- } /*minint*/
-
-
- void moveleft (psource, pdest, length) void *psource, *pdest; long length; {
-
- /*
- do a mass memory move with the left edge leading. good for closing
- up a gap in a buffer, among other things╔
- */
-
- register char *ps, *pd;
- register long ctloops;
-
- ctloops = length;
-
- if (ctloops > 0) {
-
- ps = psource; /*copy into a register*/
-
- pd = pdest; /*copy into a register*/
-
- while (ctloops--) *pd++ = *ps++;
- }
- } /*moveleft*/
-
-
- void moveright (psource, pdest, length) void *psource, *pdest; long length; {
-
- /*
- do a mass memory move with the right edge leading. good for opening
- up a gap in a buffer, among other things╔
- */
-
- register char *ps, *pd;
- register long ctloops;
-
- ctloops = length;
-
- if (ctloops > 0) {
-
- ps = (char *) psource + length - 1; /*right edge of source*/
-
- pd = (char *) pdest + length - 1; /*right edge of destination*/
-
- while (ctloops--) *pd-- = *ps--;
- }
- } /*moveright*/
-
-
- void fillchar (pfill, ctfill, chfill) void *pfill; long ctfill; char chfill; {
-
- /*
- do a mass memory fill -- copy ctfill chfills at pfill.
- */
-
- register char *p = pfill;
- register long ct = ctfill;
- register char ch = chfill;
-
- while (ct--) *p++ = (char) ch; /*tight loop*/
- } /*fillchar*/
-
-
- void clearbytes (pclear, ctclear) void *pclear; long ctclear; {
-
- /*
- fill memory with 0's.
- */
-
- fillchar (pclear, ctclear, (char) 0);
- } /*clearbytes*/
-
-
- void disposehandle (h) Handle h; {
-
- /*
- our own bottleneck for this built-in. we don't require type coercion
- and we check if its nil.
- */
-
- if (h != nil)
- DisposHandle (h);
- } /*disposehandle*/
-
-
- boolean copyhandle (horig, hcopy) Handle horig, *hcopy; {
-
- register Handle h;
- register long ct;
-
- h = NewHandle (ct = GetHandleSize (horig));
-
- if (h == nil)
- return (false);
-
- moveleft (*horig, *h, ct);
-
- *hcopy = h;
-
- return (true);
- } /*copyhandle*/
-
-
- boolean enlargehandle (hgrow, ctgrow, newdata) Handle hgrow; long ctgrow; ptrchar newdata; {
-
- /*
- make the handle big enough to hold the new data, and move the new data in
- at the end of the newly enlarged handle.
- */
-
- register Handle h = hgrow;
- register long ct = ctgrow;
- register ptrchar p = newdata;
- register long origsize;
-
- origsize = GetHandleSize (h);
-
- SetHandleSize (h, origsize + ct);
-
- if (MemError () != noErr)
- return (false);
-
- moveleft (p, *h + origsize, ct);
-
- return (true);
- } /*enlargehandle*/
-
-
- boolean loadfromhandle (hload, ixload, ctload, pdata) Handle hload; long *ixload, ctload; ptrchar pdata; {
-
- /*
- copy the next ctload bytes from hload into pdata and increment the index.
-
- return false if there aren't enough bytes.
-
- start ixload at 0.
- */
-
- register Handle h = hload;
- register ptrchar p = pdata;
- register long ct = ctload;
- register long ix = *ixload;
- register long size;
-
- size = GetHandleSize (h);
-
- if ((ix + ct) > size) /*asked for more bytes than there are*/
- return (false);
-
- moveleft (*h + ix, p, ct); /*copy out of the handle*/
-
- *ixload = ix + ct; /*increment the index into the handle*/
-
- return (true);
- } /*loadfromhandle*/
-
-
- boolean newtexthandle (bs, htext) bigstring bs; Handle *htext; {
-
- /*
- create a new handle to hold the text of the string.
-
- if the string is "\pABC" -- you get a handle of size 3.
- */
-
- register long len;
- register Handle h;
-
- h = NewHandle (len = stringlength (bs));
-
- if (h == nil)
- return (false);
-
- if (len > 0)
- moveleft (&bs [1], *h, len);
-
- *htext = h; /*pass handle back to caller*/
-
- return (true);
- } /*newtexthandle*/
-
-
- boolean pushtexthandle (bs, htext) bigstring bs; Handle htext; {
-
- /*
- htext is a handle created with newtexthandle.
-
- increase the size of the handle so we can push the text of bs at
- the end of the handle (not including length byte).
- */
-
- return (enlargehandle (htext, (long) stringlength (bs), (ptrchar) bs + 1));
- } /*pushtexthandle*/
-
-
- void texthandletostring (Handle htext, bigstring bs) {
-
- register long len;
-
- len = GetHandleSize (htext);
-
- if (len > lenbigstring)
- len = lenbigstring;
-
- setstringlength (bs, len);
-
- moveleft (*htext, &bs [1], len);
- } /*texthandletostring*/
-
-
- boolean equalstrings (bs1, bs2) bigstring bs1, bs2; {
-
- /*
- return true if the two strings (pascal type, with length-byte) are
- equal. return false otherwise.
- */
-
- register ptrchar p1 = (ptrchar) bs1, p2 = (ptrchar) bs2;
- register char ct = *p1 + 1;
-
- while (ct--)
-
- if (*p1++ != *p2++)
-
- return (false);
-
- return (true); /*loop terminated*/
- } /*equalstrings*/
-
-
- boolean unicaseequalstrings (bs1, bs2) bigstring bs1, bs2; {
-
- bigstring bscopy1, bscopy2;
-
- copystring (bs1, bscopy1);
-
- copystring (bs2, bscopy2);
-
- alllower (bscopy1);
-
- alllower (bscopy2);
-
- return (equalstrings (bscopy1, bscopy2));
- } /*unicaseequalstrings*/
-
-
- void copystring (bssource, bsdest) void *bssource, *bsdest; {
-
- /*
- create a copy of bssource in bsdest. copy the length byte and
- all the characters in the source string.
-
- assume the strings are pascal strings, with the length byte in
- the first character of the string.
- */
-
- register short i, len;
-
- len = (short) ((char *) bssource) [0];
-
- for (i = 0; i <= len; i++)
- ((char *) bsdest) [i] = ((char *) bssource) [i];
- } /*copystring*/
-
-
- boolean pushstring (bssource, bsdest) bigstring bssource, bsdest; {
-
- /*
- insert the source string at the end of the destination string.
-
- assume the strings are pascal strings, with the length byte in
- the first character of the string.
- */
-
- register short lensource = stringlength (bssource);
- register short lendest = stringlength (bsdest);
- register char *psource, *pdest;
-
- if ((lensource + lendest) > lenbigstring)
- return (false);
-
- pdest = (ptrchar) bsdest + (char) lendest + 1;
-
- psource = (ptrchar) bssource + 1;
-
- bsdest [0] += (char) lensource;
-
- while (lensource--) *pdest++ = *psource++;
-
- return (true);
- } /*pushstring*/
-
-
- boolean pushspace (bs) bigstring bs; {
-
- bigstring bsspace;
-
- setstringlength (bsspace, 1);
-
- bsspace [1] = ' ';
-
- return (pushstring (bsspace, bs));
- } /*pushspace*/
-
-
- void pushlong (num, bsdest) long num; bigstring bsdest; {
-
- bigstring bsint;
-
- NumToString (num, bsint);
-
- pushstring (bsint, bsdest);
- } /*pushlong*/
-
-
- void pushint (num, bsdest) short num; bigstring bsdest; {
-
- pushlong ((long) num, bsdest);
- } /*pushint*/
-
-
- void allupper (bs) bigstring bs; {
-
- register char len = bs [0];
- register ptrchar p = (ptrchar) &bs [1];
- register char ch;
-
- while (len--) {
-
- ch = *p;
-
- if ((ch >= 'a') && (ch <= 'z'))
- *p -= 32;
-
- p++;
- } /*while*/
- } /*allupper*/
-
-
- void alllower (bs) bigstring bs; {
-
- register char len = bs [0];
- register ptrchar p = (ptrchar) &bs [1];
- register char ch;
-
- while (len--) {
-
- ch = *p;
-
- if ((ch >= 'A') && (ch <= 'Z'))
- *p += 32;
-
- p++;
- } /*while*/
- } /*alllower*/
-
-
- boolean stringlessthan (bs1, bs2) bigstring bs1, bs2; {
-
- register short i, ctloops;
- register char ch1, ch2;
- short len1, len2;
-
- len1 = (short) bs1 [0];
-
- len2 = (short) bs2 [0];
-
- ctloops = minint (len1, len2);
-
- for (i = 1; i <= ctloops; i++) {
-
- ch1 = bs1 [i];
-
- ch2 = bs2 [i];
-
- if (ch1 != ch2) /*we have our answer*/
- return (ch1 < ch2);
- } /*for*/
-
- return (len1 < len2); /*loop terminated, strings are equal up to the min length*/
- } /*stringlessthan*/
-
-
- void midstring (bssource, ix, len, bsdest) bigstring bssource, bsdest; short ix, len; {
-
- setstringlength (bsdest, len);
-
- moveleft (bssource + ix, bsdest + 1, (long) len);
- } /*midstring*/
-
-
- boolean pushchar (ch, bs) byte ch; bigstring bs; {
-
- /*
- insert the character at the end of a pascal string.
- */
-
- register short len;
-
- len = bs [0];
-
- if (len >= lenbigstring)
- return (false);
-
- bs [++len] = ch;
-
- bs [0] = len;
-
- return (true);
- } /*pushchar*/
-
-
- boolean scanstring (ch, bs, ix) byte ch; bigstring bs; short *ix; {
-
- /*
- return in ix the index in the string of the first occurence of chscan.
-
- return false if it wasn't found, true otherwise.
-
- dmb 10/26/90: p is now initialized correctly to bs + i, not bs + 1
- */
-
- register short i;
- register ptrbyte p;
- register byte c = ch;
- register short len = stringlength (bs);
-
- for (i = *ix, p = bs + i; i <= len; i++)
-
- if (*p++ == c) {
-
- *ix = i;
-
- return (true);
- }
-
- return (false);
- } /*scanstring*/
-
-
- boolean deletestring (bs, ixdelete, ctdelete) bigstring bs; short ixdelete, ctdelete; {
-
- /*
- delete ct chars in the indicated string, starting with the character
- at offset ix.
- */
-
- register short ix = ixdelete;
- register short ct = ctdelete;
- register short len = stringlength (bs);
- register long ctmove;
- register ptrbyte pfrom, pto;
-
- if ((ix > len) || (ix < 1))
- return (false);
-
- if (ct <= 0)
- return (ct == 0);
-
- ctmove = len - ix - ct + 1;
-
- if (ctmove > 0) {
-
- pfrom = bs + ix + ct;
-
- pto = bs + ix;
-
- moveleft (pfrom, pto, ctmove);
- }
-
- setstringlength (bs, len - ct);
-
- return (true);
- } /*deletestring*/
-
-
- boolean firstword (bssource, chdelim, bsdest) bigstring bssource, bsdest; char chdelim; {
-
- /*
- copy the first word from bs, and put it into bsdest.
-
- search forwards from the beginning of the source string until you
- find chdelim.
- */
-
- register short len = stringlength (bssource);
- register short i;
- register char ch = chdelim;
-
- for (i = 1; i <= len; i++) {
-
- if (bssource [i] == ch) {
-
- midstring (bssource, 1, i - 1, bsdest);
-
- return (true);
- }
- } /*for*/
-
- copystring (bssource, bsdest);
-
- return (true);
- } /*firstword*/
-
-
- boolean lastword (bssource, chdelim, bsdest) bigstring bssource, bsdest; char chdelim; {
-
- /*
- copy the last word from bs, and put it into bsdest.
-
- search backwards from the end of the source string until you find
- chdelim.
- */
-
- register short len = stringlength (bssource);
- register short i;
- register char ch = chdelim;
-
- for (i = len; i > 0; i--) {
-
- if (bssource [i] == ch) {
-
- midstring (bssource, i + 1, len - i, bsdest);
-
- return (true);
- }
- } /*for*/
-
- copystring (bssource, bsdest);
-
- return (true);
- } /*lastword*/
-
-
- void ellipsize (bs, width) Str255 bs; short width; {
-
- /*
- if the string fits inside the given number of pixels, fine -- do nothing
- and return.
-
- if not, return a string that does fit, with ellipses representing the
- deleted characters. ellipses are generated by pressing option-semicolon.
- */
-
- register char len;
- register short newwidth;
-
- if ((newwidth = StringWidth (bs)) <= width) /*nothing to do, the string fits*/
- return;
-
- len = bs [0]; /* current length in characters*/
-
- width -= CharWidth ('╔'); /* subtract width of ellipses*/
-
- do { /*until it fits (or we run out of characters)*/
-
- newwidth -= CharWidth (bs [len]);
-
- --len;
- } while ((newwidth > width) && (len != 0));
-
- ++len; /*make room for the ellipses*/
-
- bs [len] = '╔';
-
- bs [0] = (char) len;
- } /*ellipsize*/
-
-
- void centerstring (r, bs) Rect r; bigstring bs; {
-
- /*
- draw the string in the current font, size and style, centered inside
- the indicated rectangle.
- */
-
- register short lh = globalfontinfo.ascent + globalfontinfo.descent; /*line height*/
- register short rh = r.bottom - r.top;
- register short rw = r.right - r.left;
- register short h, v;
-
- ellipsize (bs, rw); /*make sure it fits inside the rectangle, width-wise*/
-
- h = r.left + ((rw - StringWidth (bs)) / 2);
-
- v = r.top + ((rh - lh) / 2) + globalfontinfo.ascent;
-
- MoveTo (h, v);
-
- pushclip (r);
-
- DrawString (bs);
-
- popclip ();
- } /*centerstring*/
-
-
- void grayrect (r) Rect r; {
-
- pushpen ();
-
- PenMode (patBic);
-
- PenPat (quickdrawglobal (gray));
-
- PaintRect (&r);
-
- poppen ();
- } /*grayrect*/
-
-
- void centerwindow (w, rscreen) WindowPtr w; Rect rscreen; {
-
- short h, v;
- Rect r;
- short minv;
-
- r = (*w).portRect;
-
- h = rscreen.left + (((rscreen.right - rscreen.left) - (r.right - r.left)) / 2);
-
- v = rscreen.top + (((rscreen.bottom - rscreen.top) - (r.bottom - r.top)) / 6);
-
- minv = getmenubarheight () + doctitlebarheight + 10;
-
- if (v < minv)
- v = minv;
-
- MoveWindow (w, h, v, false);
- } /*centerwindow*/
-
-
- DialogPtr newmodaldialog (short id, short bolditem) {
-
- register DialogPtr pdialog;
-
- pdialog = GetNewDialog (id, nil, (DialogPtr) -1L);
-
- if (pdialog == nil)
- return (nil);
-
- centerwindow (pdialog, quickdrawglobal (screenBits).bounds);
-
- ShowWindow (pdialog);
-
- if (bolditem > 0)
- boldenbutton (pdialog, bolditem);
-
- return (pdialog);
- } /*newmodaldialog*/
-
-
- void boldenbutton (pdialog, itemnumber) DialogPtr pdialog; short itemnumber; {
-
- /*
- draw a thick black ring around the OK button in the dialog.
- */
-
- PenState savePen;
- short itemtype;
- Handle itemhandle;
- Rect itemrect;
-
- pushmacport (pdialog);
-
- GetPenState (&savePen); /*save the old pen state*/
-
- GetDItem (pdialog, itemnumber, &itemtype, &itemhandle, &itemrect); /*get the item╒s rect*/
-
- InsetRect (&itemrect, -4, -4);
-
- PenSize (3, 3); /*make the pen fatter*/
-
- FrameRoundRect (&itemrect, 16, 16); /*draw the ring*/
-
- SetPenState (&savePen); /*restore the pen state*/
-
- popmacport ();
- } /*boldenbutton*/
-
-
- short runmodaldialog () {
-
- short itemnumber;
-
- ModalDialog (nil, &itemnumber);
-
- return (itemnumber);
- } /*runmodaldialog*/
-
-
- void setdialogtext (pdialog, itemnumber, bs) DialogPtr pdialog; short itemnumber; bigstring bs; {
-
- short itemtype;
- Handle itemhandle;
- Rect itemrect;
-
- GetDItem (pdialog, itemnumber, &itemtype, &itemhandle, &itemrect);
-
- SetIText (itemhandle, bs);
- } /*setdialogtext*/
-
-
- void getdialogtext (pdialog, itemnumber, bs) DialogPtr pdialog; short itemnumber; bigstring bs; {
-
- short itemtype;
- Handle itemhandle;
- Rect itemrect;
-
- GetDItem (pdialog, itemnumber, &itemtype, &itemhandle, &itemrect);
-
- GetIText (itemhandle, bs);
- } /*getdialogtext*/
-
-
- static void selectdialogtext (DialogPtr pdialog, short itemnumber) {
-
- SelIText (pdialog, itemnumber, 0, infinity); /*select all text*/
- } /*selectdialogtext*/
-
-
- void disabledialogitem (pdialog, itemnumber) DialogPtr pdialog; short itemnumber; {
-
- short itemtype;
- Handle itemhandle;
- Rect itemrect;
-
- GetDItem (pdialog, itemnumber, &itemtype, &itemhandle, &itemrect);
-
- if (itemtype < itemDisable) { /*it is enabled, disable it*/
-
- SetDItem (pdialog, itemnumber, itemtype + itemDisable, itemhandle, &itemrect);
-
- grayrect (itemrect);
- }
- } /*disabledialogitem*/
-
-
- void enabledialogitem (pdialog, itemnumber) DialogPtr pdialog; short itemnumber; {
-
- short itemtype;
- Handle itemhandle;
- Rect itemrect;
-
- GetDItem (pdialog, itemnumber, &itemtype, &itemhandle, &itemrect);
-
- if (itemtype >= itemDisable) /*it is disabled, enable it*/
-
- SetDItem (pdialog, itemnumber, itemtype - itemDisable, itemhandle, &itemrect);
- } /*disabledialogitem*/
-
-
- void dialoggetobjectrect (pdialog, objectnumber, r) DialogPtr pdialog; short objectnumber; Rect *r; {
-
- short itemtype;
- Handle itemhandle;
-
- GetDItem (pdialog, objectnumber, &itemtype, &itemhandle, r);
- } /*dialoggetobjectrect*/
-
-
- boolean cometofront () {
-
- register short i;
- ProcessSerialNumber psn;
- EventRecord ev;
-
- GetCurrentProcess (&psn);
-
- SetFrontProcess (&psn);
-
- for (i = 1; i <= 3; i++)
- WaitNextEvent (nullEvent, &ev, 1, nil);
- } /*cometofront*/
-
-
- void arrowcursor () {
-
- SetCursor (&arrow);
- } /*arrowcursor*/
-
-
- void watchcursor () {
-
- register CursHandle hcursor;
-
- hcursor = GetCursor (watchCursor);
-
- if (hcursor != nil)
- SetCursor (*hcursor);
- } /*watchcursor*/
-
-
- void parsedialogstring (bigstring bs) {
-
- register short i;
-
- for (i = 1; i <= stringlength (bs); i++) {
-
- if (bs [i] == '¿')
- bs [i] = (char) 13;
- } /*for*/
- } /*parsedialogstring*/
-
-
- boolean alertdialog (bs) bigstring bs; {
-
- #define alertdialogid 128
- #define alertokitem 1
- #define alertmsgitem 3
- register DialogPtr pdialog;
-
- cometofront ();
-
- sysbeep;
-
- arrowcursor ();
-
- parsedialogstring (bs);
-
- if ((pdialog = newmodaldialog (alertdialogid, alertokitem)) == nil)
- return (false);
-
- setdialogtext (pdialog, alertmsgitem, bs);
-
- ShowWindow (pdialog);
-
- runmodaldialog ();
-
- DisposDialog (pdialog);
-
- return (true);
- } /*alertdialog*/
-
-
- boolean msgdialog (bs) bigstring bs; {
-
- #define msgdialogid 129
- #define msgokitem 1
- #define msgcancelitem 2
- #define msgmsgitem 3
- register DialogPtr pdialog;
- register short item;
-
- cometofront ();
-
- arrowcursor ();
-
- parsedialogstring (bs);
-
- if ((pdialog = newmodaldialog (msgdialogid, msgokitem)) == nil)
- return (false);
-
- setdialogtext (pdialog, msgmsgitem, bs);
-
- ShowWindow (pdialog);
-
- item = runmodaldialog ();
-
- DisposDialog (pdialog);
-
- return (item == msgokitem);
- } /*msgdialog*/
-
-
- boolean askdialog (bigstring bsprompt, bigstring bsanswer) {
-
- /*
- put up the standard "ask" dialog, with the provided prompt and return
- true if the user clicked on ok. the answer is in bsanswer.
- */
-
- #define askdialogid 130
- #define askokitem 1
- #define askcancelitem 2
- #define askpromptitem 3
- #define askansweritem 4
- register DialogPtr pdialog;
- register short itemnumber;
-
- ParamText (bsprompt, emptystring, emptystring, emptystring);
-
- if ((pdialog = newmodaldialog (askdialogid, askokitem)) == nil)
- return (false);
-
- setdialogtext (pdialog, askansweritem, bsanswer);
-
- selectdialogtext (pdialog, askansweritem);
-
- ShowWindow (pdialog);
-
- itemnumber = runmodaldialog ();
-
- getdialogtext (pdialog, askansweritem, bsanswer);
-
- DisposDialog (pdialog);
-
- return (itemnumber == askokitem);
- } /*askdialog*/
-
-
- boolean sfdialog (flput, fname, vnum, filetype)
-
- /*
- return true if the user selected a file with one of the SF routines,
- return false otherwise.
- */
-
- boolean flput; bigstring fname; short *vnum; OSType filetype; {
-
- register DialogTHndl hdialog;
- register short id;
- Rect r, rscreen;
- Point pt;
- SFReply reply;
- SFTypeList typesrec;
-
- cometofront ();
-
- arrowcursor ();
-
- if (flput)
- id = getDlgID;
- else
- id = putDlgID;
-
- hdialog = (DialogTHndl) GetResource ('DLOG', id);
-
- if (hdialog == nil) {
-
- pt.h = pt.v = 85;
- }
- else {
- r = (**hdialog).boundsRect;
-
- rscreen = quickdrawglobal (screenBits).bounds;
-
- pt.h = rscreen.left + (((rscreen.right - rscreen.left) - (r.right - r.left)) / 2);
-
- pt.v = rscreen.top + ((rscreen.bottom - rscreen.top) - (r.bottom - r.top)) / 3;
- }
-
- if (flput)
- SFPutFile (pt, (ptrstring) "\p", fname, nil, &reply);
-
- else {
- typesrec [0] = filetype;
-
- SFGetFile (pt, (ptrstring) "\p", nil, 1, typesrec, nil, &reply);
- }
-
- if (reply.good) {
-
- copystring (reply.fName, fname);
-
- *vnum = reply.vRefNum;
- }
-
- return (reply.good);
- } /*sfdialog*/
-
-
- void delayseconds (ct) short ct; {
-
- register long tc;
-
- tc = TickCount () + (60 * ct);
-
- while (TickCount () < tc) {}
- } /*delayseconds*/
-
-
- boolean newclearhandle (ctbytes, hreturned) long ctbytes; Handle *hreturned; {
-
- register long ct = ctbytes;
- register Handle h;
-
- *hreturned = h = NewHandle (ct);
-
- if (h == nil)
- return (false);
-
- clearbytes (*h, ct);
-
- *hreturned = h;
-
- return (true);
- } /*newclearhandle*/
-
-
- boolean newfilledhandle (ptrvoid pdata, long size, Handle *hreturned) {
-
- register Handle h;
- register long ctbytes;
-
- ctbytes = size;
-
- h = NewHandle (ctbytes);
-
- if (h == nil) {
-
- *hreturned = nil;
-
- return (false);
- }
-
- moveleft (pdata, *h, ctbytes);
-
- *hreturned = h;
-
- return (true);
- } /*newfilledhandle*/
-
-
- boolean newheapstring (bigstring bs, hdlstring *hstring) {
-
- return (newfilledhandle (bs, (long) stringlength (bs) + 1, (Handle *) hstring));
- } /*newheapstring*/
-
-
- void copyheapstring (hdlstring hstring, bigstring bs) {
-
- if (hstring == nil) {
-
- setstringlength (bs, 0);
-
- return;
- }
-
- lockhandle ((Handle) hstring);
-
- copystring (*hstring, bs);
-
- unlockhandle ((Handle) hstring);
- } /*copyheapstring*/
-
-
- static boolean keydown (short keycode) {
-
- KeyMap keys;
-
- GetKeys (&keys);
-
- return (BitTst (&keys, keycode));
- } /*keydown*/
-
-
- boolean filedelete (bspath, vnum) bigstring bspath; short vnum; {
-
- HParamBlockRec pb;
-
- clearbytes (&pb, longsizeof (pb));
-
- pb.ioParam.ioNamePtr = bspath;
-
- pb.ioParam.ioVRefNum = vnum;
-
- return (PBHDelete (&pb, false) == noErr);
- } /*filedelete*/
-
-
- void fileclose (fnum) short fnum; {
-
- if (fnum != 0)
- FSClose (fnum);
- } /*fileclose*/
-
-
- boolean fileseteof (fnum, eof) short fnum; long eof; {
-
- OSErr errcode;
-
- if (fnum != 0) {
-
- errcode = SetEOF (fnum, eof);
-
- return (errcode == noErr);
- }
-
- return (true);
- } /*fileseteof*/
-
-
- boolean fileopenorcreate (bs, vnum, creator, filetype, fnum) bigstring bs; short vnum; OSType creator, filetype; short *fnum; {
-
- /*
- open or create a file indicated by bs and vnum. if bs is a full path,
- set vnum to 0. return with fnum set to the Mac filesystem's file number
- for the file.
-
- if we open a file, we takes what we get in the creator and filetype department.
- however, if we create a file it's of the indicated filetype and creator.
- */
-
- if (FSOpen (bs, vnum, fnum) == noErr) /*file exists and is open*/
- return (true);
-
- if (Create (bs, vnum, creator, filetype) != noErr)
- return (false);
-
- if (FSOpen (bs, vnum, fnum) != noErr) {
-
- FSClose (*fnum);
-
- filedelete (bs, vnum);
-
- return (false); /*failed to open the file for writing*/
- }
-
- return (true);
- } /*fileopenorcreate*/
-
-
- boolean fileopen (bs, vnum, fnum) bigstring bs; short vnum; short *fnum; {
-
- register OSErr ec;
-
- ec = FSOpen (bs, vnum, fnum);
-
- return (ec == noErr);
- } /*fileopen*/
-
-
- boolean filenew (bsfname, vnum, creator, filetype, fnum) bigstring bsfname; short vnum; OSType creator, filetype; short *fnum; {
-
- register OSErr errcode;
-
- if (FSOpen (bsfname, vnum, fnum) == noErr) { /*file exists, delete it*/
-
- FSClose (*fnum);
-
- filedelete (bsfname, vnum);
- }
-
- errcode = Create (bsfname, vnum, creator, filetype);
-
- if (oserror (errcode)) /*failed to open the file for writing*/
- return (false);
-
- errcode = FSOpen (bsfname, vnum, fnum);
-
- if (oserror (errcode)) {
-
- FSClose (*fnum);
-
- filedelete (bsfname, vnum);
-
- return (false); /*failed to open the file for writing*/
- }
-
- return (true); /*file exists and its open*/
- } /*filenew*/
-
-
- boolean oserror (errcode) OSErr errcode; {
-
- bigstring bs;
-
- if (errcode == noErr)
- return (false);
-
- copystring ("\pMacintosh OS Error = ", bs);
-
- pushint (errcode, bs);
-
- alertdialog (bs);
-
- return (true);
- } /*oserror*/
-
-
- boolean filetruncate (fnum) short fnum; {
-
- return (SetEOF (fnum, 0) == noErr);
- } /*filetruncate*/
-
-
- boolean filewrite (fnum, ctwrite, buffer) short fnum; long ctwrite; void *buffer; {
-
- /*
- write ctwrite bytes from buffer to the current position in file number
- fnum. return true iff successful.
- */
-
- if (ctwrite > 0)
-
- if (FSWrite (fnum, &ctwrite, buffer) != noErr)
-
- return (false);
-
- return (true);
- } /*filewrite*/
-
-
- boolean fileread (fnum, ctread, buffer) short fnum; long ctread; void *buffer; {
-
- /*
- read ctread bytes from the current position in file number fnum into
- the buffer. return true iff successful.
- */
-
- if (ctread > 0)
-
- if (oserror (FSRead (fnum, &ctread, buffer)))
-
- return (false);
-
- return (true);
- } /*fileread*/
-
-
- boolean filegetchar (fnum, buffer) short fnum; byte *buffer; {
-
- /*
- read the next character from the indicated file, returning it in *buffer.
-
- return false if we're at the end of the file, without triggering an error
- dialog.
- */
-
- long fpos, eof;
-
- if (GetFPos (fnum, &fpos) != noErr)
- return (false);
-
- if (GetEOF (fnum, &eof) != noErr)
- return (false);
-
- if (fpos == eof)
- return (false);
-
- if (!fileread (fnum, 1L, buffer))
- return (false);
-
- return (true);
- } /*filegetchar*/
-
-
- boolean filewritehandle (fnum, h) short fnum; Handle h; {
-
- /*
- write the indicated handle to the open file indicated by fnum at the
- current position in the file.
- */
-
- return (filewrite (fnum, GetHandleSize (h), *h));
- } /*filewritehandle*/
-
-
- boolean filereadhandle (fnum, ctbytes, hreturned) short fnum; long ctbytes; Handle *hreturned; {
-
- register Handle h;
-
- if (!newclearhandle (ctbytes, hreturned))
- return (false);
-
- h = *hreturned; /*copy into register*/
-
- if (!fileread (fnum, ctbytes, *h)) {
-
- disposehandle (h);
-
- return (false);
- }
-
- return (true);
- } /*filereadhandle*/
-
-
- void setmenuitemenable (hmenu, item, fl) MenuHandle hmenu; short item; boolean fl; {
-
- /*
- enable or disable a menu or a menu item. if fl is true we enable the item,
- if false we disable it.
-
- if item == 0 we enable or disable the entire menu.
-
- dmb 8/1/90: check for dummy items (negative item numbers)
- */
-
- if (item < 0) /*this item has been dummied out -- do nothing*/
- return;
-
- if (fl)
- EnableItem (hmenu, item);
- else
- DisableItem (hmenu, item);
- } /*setmenuitemenable*/
-
-
- void disablemenuitem (hmenu, item) MenuHandle hmenu; short item; {
-
- setmenuitemenable (hmenu, item, false);
- } /*disablemenuitem*/
-
-
- void enablemenuitem (hmenu, item) MenuHandle hmenu; short item; {
-
- setmenuitemenable (hmenu, item, true);
- } /*enablemenuitem*/
-
-
- boolean fileparsevolname (bspath, vnum) bigstring bspath; short *vnum; {
-
- /*
- convert a full path, which might contain a volume name at the beginning
- to a path with no volume name, and it's associated volume number in vnum.
-
- example: "Rover¬:MORE Work" will return with bspath = "MORE Work" and
- vnum = -2 (the Macintosh vrefnum for the second mounted drive).
-
- this combination of information plugs nicely into a lot of the file
- manager routines.
- */
-
- short ix = 1;
- bigstring bsvolname;
- HParamBlockRec pb;
- short vrefnum;
- bigstring bs;
-
- copystring (bspath, bs); /*work on a copy*/
-
- if (isemptystring (bs))
- return (false);
-
- if (!scanstring (':', bs, &ix)) { /*no colon, the whole thing is a volname*/
-
- copystring (bs, bsvolname);
-
- pushchar (':', bsvolname);
-
- setemptystring (bs);
- }
- else {
- midstring (bs, 1, ix, bsvolname); /*pick off the vol name and the colon*/
-
- deletestring (bs, 1, ix);
- }
-
- clearbytes (&pb, longsizeof (pb));
-
- pb.volumeParam.ioNamePtr = bsvolname;
-
- pb.volumeParam.ioVolIndex = -1; /*force him to use the name pointer only*/
-
- if (PBGetVInfo (&pb, false) != noErr)
- return (false);
-
- *vnum = pb.volumeParam.ioVRefNum;
-
- return (true);
- } /*fileparsevolname*/
-
-
- boolean filefrompath (path, fname) bigstring path, fname; {
-
- /*
- return all the characters to the right of the colon in the path.
-
- example: "Work Disk #1:MORE Work:Status Center" returns "Status Center".
- */
-
- return (lastword (path, ':', fname));
- } /*filefrompath*/
-
-
- boolean folderfrompath (path, folder) bigstring path, folder; {
-
- /*
- return all the characters to the left of the colon, and the colon.
-
- example: "Work Disk #1:MORE Work:Status Center" returns "Work Disk #1:MORE Work:".
- */
-
- bigstring bs;
-
- lastword (path, ':', bs); /*kind of inefficient, but ensures symmetry*/
-
- copystring (path, folder);
-
- setstringlength (folder, stringlength (folder) - stringlength (bs));
-
- return (true);
- } /*folderfrompath*/
-
-
- boolean pathtofileinfo (path, fname, vnum) bigstring path, fname; short *vnum; {
-
- /*
- convert a Macintosh file path into a file name and a volume number.
-
- we also get the directory id of the parent directory, primarily because we
- have the code here, it supposedly works, and we might need it someday.
- */
-
- CInfoPBRec pb;
- bigstring folder;
- long idparent;
-
- if (!fileparsevolname (path, vnum)) /*no volume specified*/
- return (false);
-
- folderfrompath (path, folder);
-
- filefrompath (path, fname);
-
- clearbytes (&pb, longsizeof (pb));
-
- pb.dirInfo.ioNamePtr = folder;
-
- if (PBGetCatInfo (&pb,false) != noErr)
- return (false);
-
- if (BitTst (&pb.hFileInfo.ioFlAttrib, 3)) /*it's a folder*/
- idparent = pb.dirInfo.ioDrDirID;
- else
- idparent = pb.hFileInfo.ioFlParID;
-
- return (true);
- } /*pathtofileinfo*/
-
-
- boolean directorytopath (DirID, vnum, path) long DirID; short vnum; bigstring path; {
-
- CInfoPBRec block;
- bigstring bsdirectory;
- OSErr errcode;
-
- setemptystring (path);
-
- clearbytes (&block, longsizeof (block));
-
- block.dirInfo.ioNamePtr = bsdirectory;
-
- block.dirInfo.ioDrParID = DirID;
-
- do {
- block.dirInfo.ioVRefNum = vnum;
-
- block.dirInfo.ioFDirIndex = -1;
-
- block.dirInfo.ioDrDirID = block.dirInfo.ioDrParID;
-
- errcode = PBGetCatInfo(&block,false);
-
- if (errcode != noErr)
- return (false);
-
- if (!pushchar (':', bsdirectory))
- return (false);
-
- if (!pushstring (path, bsdirectory))
- return (false);
-
- copystring (bsdirectory, path);
- } while (block.dirInfo.ioDrDirID != fsRtDirID);
-
- return (true);
- } /*directorytopath*/
-
-
- static boolean PathNameFromWD (long vnum, bigstring path) {
-
- /*
- PBGetWDInfo has a bug under A/UX 1.1. If vnum is a real vnum
- and not a wdRefNum, then it returns garbage. Since A/UX has only 1
- volume (in the Macintosh sense) and only 1 root directory, this can
- occur only when a file has been selected in the root directory (/).
- So we look for this and hard code the DirID and vnum.
- */
-
- WDPBRec block;
-
- clearbytes (&block, longsizeof (block));
-
- block.ioVRefNum = vnum;
-
- PBGetWDInfo (&block,false);
-
- return (directorytopath (block.ioWDDirID,block.ioWDVRefNum,path));
- } /*PathNameFromWD*/
-
-
- boolean fileinfotopath (fname, vnum, path) bigstring fname; short vnum; bigstring path; {
-
- setemptystring (path);
-
- if (!PathNameFromWD ((long) vnum, path))
- return (false);
-
- pushstring (fname, path);
-
- return (true);
- } /*fileinfotopath*/
-
-
- void lockhandle (h) Handle h; {
-
- if (h != nil)
- HLock (h);
- } /*lockhandle*/
-
-
- void unlockhandle (h) Handle h; {
-
- if (h != nil)
- HUnlock (h);
- } /*unlockhandle*/
-
-
- boolean pointinrect (Point pt, Rect r) {
-
- return (PtInRect (pt, &r));
- } /*pointinrect*/
-
-
- void validrect (Rect r) {
-
- ValidRect (&r);
- } /*validrect*/
-
-
- void zerorect (Rect *rzero) {
-
- register Rect *r = rzero;
-
- (*r).top = (*r).left = (*r).bottom = (*r).right = 0;
- } /*zerorect*/
-
-
- boolean pushemptyclip (void) {
-
- /*
- set up the clip region so that no display will occur
- */
-
- Rect r;
-
- zerorect (&r);
-
- return (pushclip (r));
- } /*pushemptyclip*/
-
-
- void globaltolocalpoint (WindowPtr w, Point *pt) {
-
- pushmacport (w);
-
- GlobalToLocal (pt);
-
- popmacport ();
- } /*globaltolocalpoint*/
-
-
- void localtoglobalpoint (WindowPtr w, Point *pt) {
-
- pushmacport (w);
-
- LocalToGlobal (pt);
-
- popmacport ();
- } /*localtoglobalpoint*/
-
-
- void scrollrect (Rect r, short dh, short dv) {
-
- /*
- a front end for the Macintosh routine that scrolls a rectangle of pixels.
- */
-
- register RgnHandle rgn;
-
- rgn = NewRgn ();
-
- ScrollRect (&r, dh, dv, rgn);
-
- InvalRgn (rgn);
-
- DisposeRgn (rgn);
- } /*scrollrect*/
-
-
- boolean equalrects (r1, r2) Rect r1, r2; {
-
- return (
- (r1.top == r2.top) && (r1.left == r2.left) &&
-
- (r1.bottom == r2.bottom) && (r1.right == r2.right));
- } /*equalrects*/
-
-
- void timestamp (stamp) long *stamp; {
-
- GetDateTime (stamp);
- } /*timestamp*/
-
-
- boolean getstringlist (short listnum, short id, bigstring bs) {
-
- /*
- the Mac routine GetIndString doesn't set ResError false when we fall off
- the end of the indicated list, so we return false if the returned string
- is of zero length.
- */
-
- GetIndString (bs, listnum, id);
-
- return (stringlength (bs) > 0);
- } /*getstringlist*/
-
-
-
-
-
-